#!/bin/bash

set -e


datacolfield="date_time"
dueDayNum="0"
execData="{{ execution_date | date_add(1) | cst_ds }}"
#execData="2024-02-06"
startDate=`date -d "${execData} -${dueDayNum} days" "+%Y-%m-%d"`
echo ${startDate}
echo ${execData}

# +-------------------------
# |   Oracle配置
# +-------------------------
#url连接
oracleUrl="jdbc:oracle:thin:@pro-tms-cluster-readonly-ora.yl.com:1521/orcl"
#主机
oracle_host="pro-tms-cluster-readonly-ora.yl.com"
#端口
oracle_port="1521"
#实例
oracle_instance="orcl"
#用户名
oracleUser="jms_tms_proxy"
#密码
oraclePassword="c7UB7mj4ho4wQVnk"

now_date=`date  "+%Y-%m-%d %H:%M:%S"`
end_time=80
run_time=`date  "+%Y%m%d%H%M%S"`
echo $run_time
flag_date=`date -d "$end_time minute"  "+%Y%m%d%H%M%S"`
echo $flag_date

# 检查存储过程执行状态

check_result_sql="
SELECT
	 CASE WHEN tmp.EXEC_STATE = 1 AND tmp.FINISH_TIME IS NOT NULL THEN 1
	      ELSE 0 END AS status
FROM (
    SELECT t.*,row_number() over(PARTITION BY t.EXEC_DATE ORDER BY t.EXEC_START_TIME desc) AS row_id
    FROM YL_JMS_TMS_EFFECTIVE_STATE t
    WHERE t.EXEC_DATE = to_date('"${execData}"','yyyy-mm-dd hh24:mi:ss')
) tmp
WHERE tmp.row_id = 1
"
echo $check_result_sql

while (( run_time <= flag_date ))
do
   echo "进入循环"
   flag=`sqoop eval --connect jdbc:oracle:thin:@//${oracle_host}:${oracle_port}/${oracle_instance} --username ${oracleUser} --password ${oraclePassword} --query "$check_result_sql" | awk '{gsub(/-/, "");print}' | sed ''/^$/d'' |awk 'NR >3 {print}' | grep -o '[0-9]' `

   echo '存储过程结果:' $flag
   if((flag==1));
     then break
    elif((flag==0));
    then echo "JOB无执行记录或未执行完成"
    else echo "JOB无执行记录或未执行完成"
   fi
   echo '未执行完成：开始睡眠'
   sleep 1m
   run_time=`date  "+%Y%m%d%H%M%S"`
   echo $run_time
done

if((flag==1));
 then
      echo "正常退出,数据同步成功,succeed!"
      exit 0
 else
      echo "异常退出,数据同步失败,failed!"
      exit 1
fi

